<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title></title>
</head>
<body>
<!--弹窗：预算编辑-->
<div th:fragment="pzcurmainDetailBudgetEdit" class="modal inmodal fade" id="pzcurmainDetailBudgetEdit_Modal"
     tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span></button>
                <h4 class="modal-title" id="pzcurmainDetailBudgetEdit_modalTitle">编辑明细</h4>
                <p id="pzcurmainDetailBudgetEdit_modalDesc" class="font-bold text-danger"></p>
            </div>
            <div class="modal-body">
                <form id="pzcurmainDetailBudgetEdit_Form">
                    <input id="pzcurmainDetailBudgetEdit_id" name="id" type="hidden"/>
                    <div class="row">
                        <div class="col-md-12">
                            <input class="i-checks" type="checkbox" id="pzcurmainDetailBudget_noBudget" name="noBudget">
                            <label for="pzcurmainDetailBudget_noBudget">不设置预算 （勾选后无需设置部门、项目、经济分类，用于待分配、单据委托）</label>
                        </div>
                        <div class="col-md-12 pzcurmainDetailBudget_noBudget_area">
                            <label for="pzcurmainDetailBudget_deptCode">预算部门</label>
                            <select class="form-control" id="pzcurmainDetailBudget_deptCode"
                                    name="deptCode"> </select>
                        </div>
                        <div class="col-md-12 pzcurmainDetailBudget_noBudget_area">
                            <label for="pzcurmainDetailBudget_projectCode">预算项目</label>
                            <select class="form-control" id="pzcurmainDetailBudget_projectCode"
                                    name="projectCode"></select>
                        </div>
                        <div class="col-md-12 pzcurmainDetailBudget_noBudget_area">
                            <label for="pzcurmainDetailBudget_cateCode">经济分类</label>
                            <select class="form-control" id="pzcurmainDetailBudget_cateCode"
                                    name="cateCode"></select>
                        </div>
                        <div class="col-md-12">
                            <label for="pzcurmainDetailBudget_money">申请金额</label>
                            <input type="text" class="form-control" id="pzcurmainDetailBudget_money" name="money">
                        </div>
                        <div class="col-md-12" th:if="${dKey!='spending'}">
                            <label for="pzcurmainDetailBudget_receivablesName">账户名称</label>
                            <input type="text" class="form-control" id="pzcurmainDetailBudget_receivablesName" name="receivablesName">
                        </div>
                        <div class="col-md-12" th:if="${dKey!='spending'}">
                            <label for="pzcurmainDetailBudget_receivablesAccount">银行账号</label>
                            <input type="text" class="form-control" id="pzcurmainDetailBudget_receivablesAccount" name="receivablesAccount">
                        </div>
                        <div class="col-md-12" th:if="${dKey!='spending'}">
                            <label for="pzcurmainDetailBudget_receivablesBank">开户行</label>
                            <input type="text" class="form-control" id="pzcurmainDetailBudget_receivablesBank" name="receivablesBank">
                        </div>
                        <div class="col-md-12">
                            <label for="pzcurmainDetailBudget_remark">费用说明</label>
                            <textarea id="pzcurmainDetailBudget_remark" name="remark" class="form-control"></textarea>
                        </div>
                    </div>
                </form>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="pzcurmainDetailBudgetEditObj.submitHandler()">
                    保存
                </button>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $(function () {
            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green',
                radioClass: 'iradio_square-green',
            });
            $('.i-checks').on('ifChanged', function (event) {
                if (event.target.checked) {
                    $('.pzcurmainDetailBudget_noBudget_area').addClass('d-none');
                    $('#pzcurmainDetailBudgetEdit_Form').validate({
                        rules: pzcurmainDetailBudgetEditObj.dataRule_noBudget,
                        submitHandler: function (form) {
                            pzcurmainDetailBudgetEditObj.dataFormSubmit(form);
                        }
                    });
                } else {
                    $('.pzcurmainDetailBudget_noBudget_area').removeClass('d-none');
                    $('#pzcurmainDetailBudgetEdit_Form').validate({
                        rules: pzcurmainDetailBudgetEditObj.dataRule,
                        submitHandler: function (form) {
                            pzcurmainDetailBudgetEditObj.dataFormSubmit(form);
                        }
                    });
                }
            });
            $('#pzcurmainDetailBudgetEdit_Form').validate({
                rules: pzcurmainDetailBudgetEditObj.dataRule,
                submitHandler: function (form) {
                    pzcurmainDetailBudgetEditObj.dataFormSubmit(form);
                }
            });
            $('#pzcurmainDetailBudget_deptCode').change(function (e) {
                $('#pzcurmainDetailBudget_projectCode').empty();
                $('#pzcurmainDetailBudget_cateCode').empty();
                pzcurmainDetailBudgetEditObj.availableProject($(this).val()).then(o => {
                    pzcurmainDetailBudgetEditObj.buildProjects(o);
                })

            })
            $('#pzcurmainDetailBudget_projectCode').change(function (e) {
                $('#pzcurmainDetailBudget_cateCode').empty();
                pzcurmainDetailBudgetEditObj.availableCate($('#pzcurmainDetailBudget_deptCode').val(), $(this).val()).then(o => {
                    pzcurmainDetailBudgetEditObj.buildCate(o[0].nodes);
                })
            })
        })
        var pzcurmainDetailBudgetEditObj = new Object({
            formData: {},
            dataRule_noBudget: {
                money: {
                    required: true,
                    number: true,
                    min: 0.01,
                    toDecimal2: true
                },
                remark: {
                    maxlength: 400
                }
            },
            dataRule: {
                deptCode: {
                    required: true
                },
                projectCode: {
                    required: true
                },
                cateCode: {
                    required: true
                },
                money: {
                    required: true,
                    number: true,
                    min: 0.01,
                    toDecimal2: true
                },
                remark: {
                    maxlength: 400
                }
            },
            init: function (id) {
                let myId = id;
                // 清除值
                $('#pzcurmainDetailBudget_deptCode').empty();
                $('#pzcurmainDetailBudget_projectCode').empty();
                $('#pzcurmainDetailBudget_cateCode').empty();
                $("#pzcurmainDetailBudgetEdit_Form")[0].reset();
                $("input[type='hidden']").val("");
                $('#pzcurmainDetailBudget_deptCode').append($(`<option value="">-- 请选择部门 --</option>`))
                pzcurmainDetailObj.dataSelect.depts.map(o => {
                    $('#pzcurmainDetailBudget_deptCode').append($(`<option value="${o.deptCode}">${o.deptName}</option>`))
                })
                if (myId) {
                    $.myGet({
                        url: `/pz/cur/pzbudget/info/${myId}/${pzcurmainDetailObj.mainId}`,
                        success: function (data) {
                            if (data && data.code === 0) {
                                pzcurmainDetailBudgetEditObj.fillForm(data.budget);
                            } else {
                                toastr.error(data.msg, '操作失败')
                            }
                        }
                    })
                }
                $('#pzcurmainDetailBudgetEdit_Modal').modal('show');
            },
            fillForm: function (data = {}) {
                if (data.noBudget) {
                    for (let i in data) {
                        if ($("#pzcurmainDetailBudgetEdit_Form").find(`[name=${i}]`).length > 0) {
                            $("#pzcurmainDetailBudgetEdit_Form").find(`[name=${i}]`).val(data[i])
                        }
                    }
                } else {
                    let proArrs = [
                        pzcurmainDetailBudgetEditObj.availableProject(data.deptCode),
                        pzcurmainDetailBudgetEditObj.availableCate(data.deptCode, data.projectCode)
                    ];
                    Promise.all(proArrs).then(values => {
                        pzcurmainDetailBudgetEditObj.buildProjects(values[0]);
                        pzcurmainDetailBudgetEditObj.buildCate(values[1][0].nodes);
                        for (let i in data) {
                            if ($("#pzcurmainDetailBudgetEdit_Form").find(`[name=${i}]`).length > 0) {
                                $("#pzcurmainDetailBudgetEdit_Form").find(`[name=${i}]`).val(data[i])
                            }
                        }
                    })
                }
            },
            buildProjects: function (projects) {
                $('#pzcurmainDetailBudget_projectCode').empty()
                $('#pzcurmainDetailBudget_projectCode').append($(`<option value="">-- 请选择预算项目 --</option>`))
                projects.map(o => {
                    $('#pzcurmainDetailBudget_projectCode').append($(`<option value="${o.code}">${o.name}</option>`))
                })
            },
            buildCate: function (cates) {
                $('#pzcurmainDetailBudget_cateCode').empty();
                $('#pzcurmainDetailBudget_cateCode').append($(`<option value="">-- 请选择经济分类 --</option>`))
                cates.map(o => {
                    $('#pzcurmainDetailBudget_cateCode').append($(`<option value="${o.code}">${o.text}</option>`))
                })
            },
            availableProject: function (deptCode) {
                return new Promise(function (resolve, reject) {
                    $.myPost({
                        url: `/portal/budget/findAvailableProject.json`,
                        data: {deptCode: deptCode},
                        success: function (data) {
                            resolve(data);
                        }
                    })
                })
            },
            availableCate: function (deptCode, projectCode) {
                return new Promise(function (resolve, reject) {
                    $.myPost({
                        url: `portal/budget/budgetCateTreeData.json`,
                        data: {
                            deptCode: deptCode,
                            projectCode: projectCode
                        },
                        success: function (data) {
                            resolve(data);
                        }
                    })
                })
            },
            submitHandler: function () {
                $("#pzcurmainDetailBudgetEdit_Form").submit();
            },
            dataFormSubmit: function (form) {
                let tip = `<ul style='text-align:left;list-style:none;'>
                    <li>经济分类："+$('#txt_cate_sel').val()+"<li/>
                    <li>费用说明："+$('#budget_remark').val()+"<li/>
                    <li>申请金额："+$('#budget_money').val()+"<li/>
                    </ul>"`;

                let formVals = $(form).serializeArray();
                let data = {}
                for (let i = 0; i < formVals.length; i++) {
                    data[formVals[i].name] = formVals[i].value;
                }
                data.mainId = pzcurmainDetailObj.mainId;
                data.noBudget = $('#pzcurmainDetailBudget_noBudget').prop('checked');
                data.money = parseFloat(data.money);
                let tipArr = [];
                tipArr.push(`申请金额：${$.moneyFormat(data.money)}`);
                if (!data.noBudget) {
                    tipArr.push(`预算部门：${$('#pzcurmainDetailBudget_deptCode option:selected').text()}`);
                    tipArr.push(`预算项目：${$('#pzcurmainDetailBudget_projectCode option:selected').text()}`);
                    tipArr.push(`经济分类：${$('#pzcurmainDetailBudget_cateCode option:selected').text()}`);
                } else {
                    delete data.deptCode;
                    delete data.projectCode;
                    delete data.cateCode;
                }
                tipArr.push(`费用说明：${data.remark}`);
                $.mySwalConfirm(tipArr.join("\r\n"), function () {
                    $.myPost({
                        url: `/pz/cur/pzbudget/budget`,
                        dataType: 'json',
                        data: data,
                        success: function (data) {
                            if (data && data.code == 0) {
                                toastr.success(data.msg, '操作提示')
                                $('#pzcurmainDetailBudgetEdit_Modal').modal('hide')
                                pzcurmainDetailBudgetListObj.refreshDataList();
                            } else {
                                toastr.error(data.msg, '操作失败')
                            }
                        }
                    })
                }, "请再次确认信息", "my-sweet");
            }
        })
    </script>

</div>
</body>
</html>